package cn.yangliu.nacos.comm;

import java.util.Objects;

import cn.yangliu.nacos.comm.ex.ResultCode;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

/**
 * The type Json result.
 *
 * <p>
 * .
 *
 * @param <T> the type parameter
 * @author 问道于盲
 * @date 2019 -05-16
 */
@Data
@NoArgsConstructor
public class JsonResult<T> implements ResultCode{

    /**
     * current class instance's member.
     * The Code.
     */
    private int code = 200;

    /**
     * current class instance's member.
     * The Msg.
     */
    private String msg = "sucess";

    /**
     * current class instance's member.
     * The Data.
     */
    private T data;

    /**
     * Instantiates a new Json result.
     * forbid other class to create a new instance.
     * only itself can create a new instance.
     *
     * @param data the data
     */
    private JsonResult(T data) {
        this.data = data;
    }

    /**
     * Instantiates a new Json result.
     * forbid other class to create a new instance.
     * only itself can create a new instance.
     *
     * @param code the code
     * @param msg  the msg
     */
    private JsonResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * Ok boolean.
     *
     * @return the boolean
     */
    public boolean ok() {
        return Objects.equals(HttpStatus.OK.value(), code);
    }

    /**
     * Get instance json result.
     *
     * @param <T>  the type parameter
     * @param data the data
     * @return the json result
     */
    public static <T> JsonResult<T> getInstance(T data){
        return new JsonResult<>(data);
    }

    /**
     * Get instance json result.
     *
     * @param <T> the type parameter
     * @return the json result
     */
    public static <T> JsonResult<T> getInstance(){
        return new JsonResult<>(null);
    }

    /**
     * Get instance json result.
     *
     * @param <T>  the type parameter
     * @param code the code
     * @param msg  the msg
     * @return the json result
     */
    public static <T> JsonResult<T> getInstance(int code, String msg){
        return new JsonResult<>(code, msg);
    }

    /**
     * current class method for getter.
     * Gets instance.
     *
     * @param <T>        the type parameter
     * @param resultCode the result code
     * @return the instance
     */
    public static <T> JsonResult<T> getInstance(ResultCode resultCode) {
        return new JsonResult<>(resultCode.getCode(), resultCode.getMsg());
    }
}
